package com.dony.web.controller.store;

import com.dony.common.annotation.Log;
import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.common.core.page.TableDataInfo;
import com.dony.common.enums.BusinessType;
import com.dony.shop.domain.Item;
import com.dony.shop.domain.vo.ItemPriceVo;
import com.dony.shop.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品Controller
 *
 * @author sundw
 * @date 2022-08-31
 */
@RestController
@RequestMapping("/store/item")
public class ShopItemController extends BaseController
{
    @Autowired
    private IItemService shopItemService;

    /**
     * 更新发布站点的所有商品的商品
     *
     * @param merchantId
     * @return
     */
    @RequestMapping("/sync/{merchantId}")
    public AjaxResult syncItemStatusByMerchant(@PathVariable Long merchantId) {
        shopItemService.syncItemStatusByMerchant(merchantId);
        return AjaxResult.success();
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "get/price/{itemId}")
    public AjaxResult getItemInfoForChangePrice(@PathVariable("itemId") Long itemId)
    {
        return AjaxResult.success(shopItemService.getItemWithPrice(itemId));
    }



    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Item item)
    {
        startPage();
        List<Item> list = shopItemService.getListByParam(item);
        return getDataTable(list);
    }


    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return AjaxResult.success(shopItemService.getById(itemId));
    }

    /**
     * 新增商品
     */
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item)
    {
        return toAjax(shopItemService.save(item));
    }

    /**
     * 修改商品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Item item)
    {
        return toAjax(shopItemService.updateById(item));
    }

    /**
     * 删除商品
     */
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(shopItemService.removeByIds(Arrays.asList(itemIds)));
    }
}
